package team.scau.laboratory_repair.web.pc;

import lombok.extern.apachecommons.CommonsLog;
import org.activiti.engine.HistoryService;
import org.activiti.engine.history.HistoricProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import team.scau.laboratory_repair.common.json.Result;
import team.scau.laboratory_repair.common.utils.spring.SpringContextHolder;
import team.scau.laboratory_repair.modules.po.Note;
import team.scau.laboratory_repair.modules.po.Record;
import team.scau.laboratory_repair.service.ActivitiService;
import team.scau.laboratory_repair.service.RepairService;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @Description: ${todo}
 * @Author: wxz1997
 * @Date 18-7-22下午7:50
 */
@Controller
@RequestMapping("/mobile/test")
@CommonsLog
public class TestController {

    @Autowired
    private ActivitiService activitiService;

    @Autowired
    private RepairService repairService;

    @GetMapping("/test1")
    public String test1(){
        return "test/test1";
    }

    @GetMapping("/test2")
    public String test2(){
        return "/test/test2";
    }

    @GetMapping("/startProc")
    public void procStart(){
        Record record = Record.builder().equipmentId("123").area(233L).detail("test").id(4L).build();
        HashMap<String, Object> data = new HashMap<>();
        data.put("recordId", record.getId());
        data.put("applicantId", 2L);
        activitiService.startProcWithData("labRepair", data);
    }

    @GetMapping("/apply")
    public void apply(){
        String id = "b6dc7a9d-d5cb-11e8-bad2-22a589259cfb";
        HashMap<String, Object> map = new HashMap<>();
        map.put("continue", true);
        activitiService.completeWithData(id, map);
    }

    @GetMapping("/delegate")
    public void delegate(){
        String id = "5e4dfbfc-d5c6-11e8-bad2-22a589259cfb";
        activitiService.complete(id);
    }

    @GetMapping("/mend")
    public void mend(){
        String id = "10dd806e-d5c7-11e8-bad2-22a589259cfb";
        activitiService.complete(id);
    }

    @GetMapping("/check")
    public void check(){
        String id = "cafb6032-d5c7-11e8-bad2-22a589259cfb";
        activitiService.complete(id);
    }

    @GetMapping("/startProc2")
    public void procStart2() throws Exception {
        Record record = Record.builder().equipmentId("123").area(1L).room("200").detail("test").build();
        repairService.submit(record, null);
    }

    @GetMapping("/claim")
    public void claim() throws Exception {
        String id = "4afb769b-e041-11e8-98d5-22a589259cfb";
        Long recordId = 67L;
        repairService.claim(id, recordId, 3L, true);
    }

    @GetMapping("/unclaim")
    public void unclaim() throws Exception {
        String id = "e625b583-db8e-11e8-a241-22a589259cfb";
        Long recordId = 7L;
        repairService.unclaim(id, recordId, true);
    }

    @GetMapping("/note")
    public void note() throws Exception {
        String id = "e625b583-db8e-11e8-a241-22a589259cfb";
        Long recordId = 7L;
        Note note = Note.builder().staff("t3").recordId(recordId).remark(new Date().toString()).build();
        repairService.note(id, recordId, note, null);
    }

    @GetMapping("/complete")
    public void complete() throws Exception {
        String id = "3226cdde-e041-11e8-98d5-22a589259cfb";
        Long recordId = 66L;
        Note note = Note.builder().recordId(recordId).staff("t2").remark("ok").build();
//        repairService.complete(id, recordId, note, true);
    }

    @PostMapping("/candidate")
    public Result selectCandidateTask() {
        Result result = new Result();

        return result;
    }

    @PostMapping("/history")
    public Result selectPersonalHistory(@RequestParam(name = "after", required = false) Date after
            , @RequestParam(name = "before", required = false)Date before
            , @RequestParam(name = "type", defaultValue = "notEnd")String type) {
        Result result = new Result();

        return result;
    }

    @GetMapping("/any")
    public void any() throws Exception{
        HistoryService historyService = SpringContextHolder.getBean(HistoryService.class);
        List<HistoricProcessInstance> list = historyService.createHistoricProcessInstanceQuery().includeProcessVariables().list();
    }

}
